﻿using DevExpress.Utils;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace EyFramework.Library
{
    public delegate void IndexChangedEventHandler(object sender, EventArgs e);

    public partial class DataNavigator : UserControl
    {
        public event IndexChangedEventHandler IndexChanged;

        private int _CurrentIndex = 0;//当前的位置

        public List<string> IDList = new List<string>();

        public int CurrentIndex
        {
            get { return _CurrentIndex; }
            set
            {
                _CurrentIndex = value;
                ChangeIndex(value);
            }
        }

        public DataNavigator()
        {
            InitializeComponent();
        }

        private void peFirst_Click(object sender, EventArgs e)
        {
            ChangeIndex(0);
        }

        private void pePrevious_Click(object sender, EventArgs e)
        {
            ChangeIndex(_CurrentIndex - 1);
        }

        private void peNext_Click(object sender, EventArgs e)
        {
            ChangeIndex(_CurrentIndex + 1);
        }

        private void peLast_Click(object sender, EventArgs e)
        {
            ChangeIndex(IDList.Count - 1);
        }

        private void ChangeIndex(int newIndex)
        {
            int count = IDList.Count;
            if (count == 0)
            {
                EnableControl(false);
                this.txtInfo.Text = "";
            }
            else
            {
                EnableControl(true);

                newIndex = (newIndex < 0) ? 0 : newIndex;
                _CurrentIndex = ((count - 1) > newIndex) ? newIndex : (count - 1);
                this.pePrevious.Enabled = (_CurrentIndex > 0);
                this.peNext.Enabled = _CurrentIndex < (count - 1);
                this.txtInfo.Text = string.Format("{0}/{1}", _CurrentIndex + 1, count);

                if (IndexChanged != null)
                {
                    IndexChanged(this, new EventArgs());
                }
            }
        }

        private void EnableControl(bool enable)
        {
            this.peFirst.Enabled = enable;
            this.peLast.Enabled = enable;
            this.peNext.Enabled = enable;
            this.pePrevious.Enabled = enable;
        }

        public void MoveNext()
        {
            if (peNext.Enabled)
            {
                ChangeIndex(_CurrentIndex + 1);
            }
        }

        public void MovePre()
        {
            if (pePrevious.Enabled)
            {
                ChangeIndex(_CurrentIndex - 1);
            }
        }

        public void MoveFirst()
        {
            if (peFirst.Enabled)
            {
                ChangeIndex(0);
            }
        }

        public void MoveLast()
        {
            if (peLast.Enabled)
            {
                ChangeIndex(IDList.Count - 1);
            }
        }

        private void toolTipController1_GetActiveObjectInfo(object sender, ToolTipControllerGetActiveObjectInfoEventArgs e)
        {
        }
    }
}